package com.three.crm.http;

import android.content.Context;
import android.util.Log;


import com.three.crm.util.AppManager;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * Created by lqd on 2022-06-15.
 */
public abstract class CommonResultCallBack extends CallBackUtil.CallBackString {

    private Context context;
    public abstract void onResult(String result,int requireCode);
    public abstract void onConnectTimeOut(Call call, Exception e, int requireCode);
    public abstract void onFail(String msg,int requireCode);

    public CommonResultCallBack(Context mcontext) {
        this.context = mcontext;
    }

    @Override
    public void onFailure(Call call, Exception e,int requireCode) {
        onConnectTimeOut(call,e,requireCode);
    }

    @Override
    public void onResponse(String response,int requireCode) {
        //Log.i("TAG","response000="+response);
        try {
            JSONObject responses = new JSONObject(response);
            if (responses.has("code")) {
                if (responses.getInt("code") == 0) {
                    String result = responses.getString("data");
                    onResult(result,requireCode);
                } else if (responses.getInt("code") == 1008) {
                    AppManager.reLogin(context);
                } else {
                    String msg = responses.getString("message");
                    onFail(msg,requireCode);
                }
            } else {
                onFail("请求异常",requireCode);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
